package edu.chinasoft.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import org.apache.log4j.Logger;

import edu.chinasoft.pojo.User;

/**
 * 
 * @author cjogng
 * @date 2020年1月19日:下午4:21:41
 * @version V1.0
 * @Description: 
 * 
*/
@WebServlet(name="myLogoutUserForServlet",value="/servlet/logoutuserforservlet")
public class  LogoutUserForServlet  extends HttpServlet {
	private static Logger logger = Logger.getLogger( LogoutUserForServlet .class);
	
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" LogoutUserForServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		HttpSession session = request.getSession(false);
		
		User user=null;
		
		if(session!=null) {
			user= (User)session.getAttribute("user");
		}
		
		if(user==null) {      //表示用户没有登录
			return;
		}else {                //表示用户登录
			session.removeAttribute("user");
			response.sendRedirect(""+request.getContextPath()+"/main.html");
		}
		logger.debug(" LogoutUserForServlet >doGet end.......");
	}
	
	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" LogoutUserForServlet >doPost start.......");
		this.doGet(request, response);
		
		logger.debug(" LogoutUserForServlet >doPost end.......");
	}
	
}